﻿/******************************
 * Author：bly QQ:384607180
 * Date:2014-08-20 20:55:51
******************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Text;
using System.IO;
using Besture.Common.Definition;

namespace Besture.Common
{
    public class SendSms
    {

        /// <summary>
        /// 发送短信
        /// </summary>
        /// <param name="sn">软件序列号</param>
        /// <param name="phone">手机号</param>
        /// <param name="content">短信内容</param>
        /// <param name="priority">优先级</param>
        /// <returns></returns>
        public string SendMsg(string sn, string phone, string content, string priority)
        {
            //即时发送      这里是软件序列号    手机号       短信内容     优先级     
            int result = EUCPComm.SendSMS(sn, phone, content, priority);
            if (result == 1)
                return "发送成功";
            else if (result == 101)
                return "网络故障";
            else if (result == 102)
                return "其它故障";
            else if (result == 0)
                return "失败";
            else if (result == 100)
                return "序列号码为空或无效";
            else if (result == 107)
                return "手机号码为空或者超过1000个";
            else if (result == 108)
                return "手机号码分割符号不正确";
            else if (result == 109)
                return "部分手机号码不正确，已删除，其余手机号码被发送";
            else if (result == 110)
                return "短信内容为空或超长（70个汉字）";
            else if (result == 201)
                return "计费失败，请充值";
            else
                return "其他故障值：" + result.ToString();

        }

        ///// <summary>
        ///// 以 HTTP 的 POST 提交方式 发送短信(ASP.NET的网页或是C#的窗体，均可使用该方法)
        ///// </summary>
        ///// <param name="mobile">要发送的手机号码</param>
        ///// <param name="msg">要发送的内容</param>
        ///// <returns>发送结果</returns>
        //public string SendMsg(string mobile, string msg)
        //{
        //    string name = AppSettings.SmsName;
        //    string pwd = AppSettings.SmsPass;//登陆web平台 http://sms.1xinxi.cn  在管理中心--基本资料--接口密码（28位） 如登陆密码修改，接口密码会发生改变，请及时修改程序
        //    string sign = AppSettings.SmsSignature;             //一般为企业简称
        //    StringBuilder arge = new StringBuilder();

        //    arge.AppendFormat("name={0}", name);
        //    arge.AppendFormat("&pwd={0}", pwd);
        //    arge.AppendFormat("&content={0}", msg);
        //    arge.AppendFormat("&mobile={0}", mobile);
        //    arge.AppendFormat("&sign={0}", sign);
        //    arge.Append("&type=pt");
        //    string weburl = "http://sms.1xinxi.cn/asmx/smsservice.aspx";

        //    string resp = PushToWeb(weburl, arge.ToString(), Encoding.UTF8);
        //    if (resp.Split(',')[0] == "0")
        //    {
        //        //提交成功
        //    }
        //    else
        //    {
        //        //提交失败，可能余额不足，或者敏感词汇等等
        //    }

        //    return resp;//是一串 以逗号隔开的字符串。阅读文档查看响应的意思
        //}

        /// <summary>
        /// HTTP POST方式
        /// </summary>
        /// <param name="weburl">POST到的网址</param>
        /// <param name="data">POST的参数及参数值</param>
        /// <param name="encode">编码方式</param>
        /// <returns></returns>
        public string PushToWeb(string weburl, string data, Encoding encode)
        {
            byte[] byteArray = encode.GetBytes(data);

            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(weburl));
            webRequest.Method = "POST";
            webRequest.ContentType = "application/x-www-form-urlencoded";
            webRequest.ContentLength = byteArray.Length;
            Stream newStream = webRequest.GetRequestStream();
            newStream.Write(byteArray, 0, byteArray.Length);
            newStream.Close();

            //接收返回信息：
            HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
            StreamReader aspx = new StreamReader(response.GetResponseStream(), encode);
            return aspx.ReadToEnd();
        }
    }
}